package org.shop.controller.app;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.shop.entity.Attention;
import org.shop.entity.security.MyUserDetail;
import org.shop.service.AttentionService;
import org.shop.util.ObjUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author Administrator
 *
 */
@Controller
public class AttentionController {

	private static Logger logger = LogManager.getLogger(AttentionController.class);
	
	@Autowired
	private AttentionService attentionService;
	
	/**
	 * 关注and取消关注
	 * @param req
	 * @return
	 */
	@RequestMapping("app/updateAttention")
	@ResponseBody
	public Map<String, Object> updateAttention(HttpServletRequest req){
		Map<String, Object> map = new HashMap<String, Object>();
		//获取登录人用户信息
		MyUserDetail ud = (MyUserDetail) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		Long targetId = Long.parseLong(req.getParameter("targetId"));
		byte attentionType = Byte.parseByte(req.getParameter("attentionType"));
		try {
			Attention attention = new Attention();
			attention.setUserId(ud.getUserId());
			attention.setTargetId(targetId);
			List<Attention> attentionList = attentionService.getAtternionList(attention);
			if(ObjUtil.isNotNullAndEmpty(attentionList)) {
				attentionService.deleteByPrimaryKey(attentionList.get(0).getAttentionId());
				map.put("state", 1);
				map.put("msg", "取消关注成功");
			}else {
				attention.setAttentionType(attentionType);
				attentionService.insertSelective(attention);
				map.put("state", 1);
				map.put("msg", "关注成功");
			}
		}catch(Exception e) {
			map.put("state", 0);
			map.put("msg", "关注失败");
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return map;
	}

	/**
	 * 查询我是否关注了此店铺
	 * @param req
	 * @return
	 */
	@RequestMapping("app/getAttention")
	@ResponseBody
	public Map<String, Object> getAttention(HttpServletRequest req) {
		Map<String, Object> map = new HashMap<String, Object>();
		//获取登录人用户信息
		MyUserDetail ud = (MyUserDetail) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		Long targetId = Long.parseLong(req.getParameter("targetId")); 
		byte attentionType = Byte.parseByte(req.getParameter("attentionType"));
		try {
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("userId", ud.getUserId());
			Attention attention = new Attention();
			attention.setUserId(ud.getUserId());
			attention.setTargetId(targetId);
			attention.setAttentionType(attentionType);
			//关注人
			req.setAttribute("attentionSUserList", attentionService.getAtternionUserList(attention));
			//关注店铺
			req.setAttribute("attentionShopList", attentionService.getAtternionShopList(attention));
			map.put("attentionSUserList", attentionService.getAtternionUserList(attention));
			map.put("attentionShopList", attentionService.getAtternionShopList(attention));
			map.put("state", 1);
			map.put("msg", "获取成功");
		}catch(Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
			map.put("state", 0);
			map.put("msg", "获取失败");
		}
		return map;
	}
	
	/**
	 * 获取我的关注
	 * @param req
	 * @return
	 */
	@RequestMapping("app/getAttentionList")
	public String getAttentionList(HttpServletRequest req) {
		//获取登录人用户信息
		MyUserDetail ud = (MyUserDetail) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		String targetId = req.getParameter("targetId"); 
		String attentionType = req.getParameter("attentionType");
		try {
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("userId", ud.getUserId());
			Attention attention = new Attention();
			attention.setUserId(ud.getUserId());
			if(ObjUtil.isNotNullAndEmpty(targetId)) {
				attention.setTargetId(Long.parseLong(targetId));
			}
			if(ObjUtil.isNotNullAndEmpty(attentionType)) {
				attention.setAttentionType(Byte.parseByte(attentionType));
			}
			//关注人
			req.setAttribute("attentionSUserList", attentionService.getAtternionUserList(attention));
			//关注店铺
			req.setAttribute("attentionShopList", attentionService.getAtternionShopList(attention));
		}catch(Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return "app/attention";
	}

	
}
